package com.bussiness.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bussiness.auto.JSONAuthentication;
import com.bussiness.entity.BaFbtask;
import com.bussiness.entity.BaNotice;
import com.bussiness.entity.BuUser;
import com.bussiness.mapper.BaNoticeMapper;
import com.bussiness.mapper.BuUserMapper;
import com.bussiness.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bussiness
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/ba-notice")
public class BaNoticeController extends JSONAuthentication {
    @Autowired
    BaNoticeMapper baNoticeMapper;
    @Autowired
    BuUserMapper buUserMapper;
    @ApiOperation(value = "添加通知")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody BaNotice baNotice) throws  Exception{
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        baNotice.setCreatTime(timestamp);
        if(baNoticeMapper.insert(baNotice)>0){
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除通知")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=baNoticeMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改通知")
    @PostMapping("/upd")
    public void updtast(HttpServletRequest request, HttpServletResponse response, @RequestBody BaNotice baNotice) throws  Exception{
        if(baNoticeMapper.updateById(baNotice)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有通知")
    @PostMapping("/findAllUser")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<BaNotice> shUserPage=new Page<>(current,size);
        Page<BaNotice> shUserPages=baNoticeMapper.selectPage(shUserPage,null);
        List<BaNotice> records = shUserPages.getRecords();
        int total=baNoticeMapper.selectCount(null);
        for (BaNotice a :records){
            a.setBuUser(buUserMapper.selectById(a.getUserId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("notice",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

